//
//  EditAddressView.swift
//  Box
//
//  Created by mn on 2025/3/31.
//

import UIKit
import SnapKit

class EditAddressView: UIView {
    
    var tapSelectRegion:((Bool) -> Void)?
    private let titleLabel: UILabel = {
        let label = UILabel()
        label.text = "联系人信息"
        label.font = UIFont.boldSystemFont(ofSize: 16)
        return label
    }()
    
    lazy var nameTextField = createTextField(placeholder: "姓名")
    lazy var phoneTextField = createTextField(placeholder: "电话")
    lazy var regionTextField = createTextField(placeholder: "省市区")
    lazy var addressTextField = createTextField(placeholder: "详细地址（街道、单元楼）")
    lazy var defaultButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage.addressSelect, for: .selected)
        button.setImage(UIImage.addressNormal, for: .normal)
        button.setTitle("  默认地址", for: .normal)
        button.setTitleColor(.fromHex("#292D32"), for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.addAction {
            [weak self] in
            self?.defaultButton.isSelected.toggle()
        }
        return button
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
    }
    
    private func setupUI() {
        backgroundColor = .white
        layer.cornerRadius = 13
        addSubview(titleLabel)
        addSubview(nameTextField)
        addSubview(phoneTextField)
        addSubview(regionTextField)
        addSubview(addressTextField)
        addSubview(defaultButton)

        titleLabel.snp.makeConstraints { make in
            make.top.left.equalToSuperview().offset(12)
        }
        
        nameTextField.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(12)
            make.left.equalToSuperview().offset(16)
            make.right.equalTo(snp.centerX).offset(-8)
            make.height.equalTo(40)
        }
        
        phoneTextField.snp.makeConstraints { make in
            make.top.equalTo(nameTextField)
            make.left.equalTo(snp.centerX).offset(8)
            make.right.equalToSuperview().offset(-12)
            make.height.equalTo(40)
        }
        regionTextField.snp.makeConstraints { make in
            make.top.equalTo(nameTextField.snp.bottom).offset(12)
            make.left.equalTo(nameTextField)
            make.right.equalToSuperview().offset(-12)
            make.height.equalTo(40)
        }
        
        addressTextField.snp.makeConstraints { make in
            make.top.equalTo(regionTextField.snp.bottom).offset(12)
            make.left.equalTo(nameTextField)
            make.right.equalToSuperview().offset(-12)
            make.height.equalTo(40)
        }
        defaultButton.snp.makeConstraints { make in
            make.top.equalTo(addressTextField.snp.bottom).offset(22)
            make.left.equalTo(nameTextField)
        }
    }
    
    private func createTextField(placeholder: String) -> UITextField {
        let textField = UITextField()
        textField.placeholder = placeholder
        textField.borderStyle = .none
        textField.backgroundColor = UIColor(white: 0.95, alpha: 1)
        textField.layer.cornerRadius = 5
        textField.font = UIFont.systemFont(ofSize: 14)
        textField.delegate = self
        textField.setLeftPadding(8)
        return textField
    }
    var data: AddressData!{
        didSet{
            defaultButton.isSelected = data.isDefault
            nameTextField.text = data.name
            phoneTextField.text = data.mobile
            addressTextField.text = data.detailAddress
            regionTextField.text = data.addressPathDes

        }
    }
}
extension EditAddressView: UITextFieldDelegate{
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        tapSelectRegion?(textField == regionTextField)
        return textField != regionTextField
    }
}

// 扩展 UITextField，增加左侧内边距
extension UITextField {
    func setLeftPadding(_ amount: CGFloat) {
        let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.height))
        self.leftView = paddingView
        self.leftViewMode = .always
    }
}
